package com.atguigu.juc.study.chapter05.locksupport.locks;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 *   await()和signal()/signalAll()必须在lock和unlock块中使用，否则抛异常
 *
 * @author : leimingchao
 * @date Date : 2022年06月12日 20:31
 */
public class LockSupportDemo5 {

    public static void main(String[] args) {

        Lock lock = new ReentrantLock();

        Condition condition = lock.newCondition();

        new Thread(() -> {
            // lock.lock();
            try {
                System.out.println(Thread.currentThread().getName() + "\t -----come in");
                condition.await();
                System.out.println(Thread.currentThread().getName() + "\t -----被唤醒");

            } catch(InterruptedException e) {
                e.printStackTrace();
            } finally {
                // lock.unlock();
            }
        }, "t1").start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(() -> {
            // lock.lock();
            try {
                condition.signal();
                System.out.println(Thread.currentThread().getName() + "\t -----发出通知");

            } finally {
                // lock.unlock();
            }
        }, "t2").start();

    }
}
